package org.digitaldomination.speedclicker;

import org.digitaldomination.speedclicker.R;
import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

public class speedclickerActivity extends Activity {
	/** Called when the activity is first created. */
	SQLiteDatabase scoreDB;
	int count = 0;
	boolean startet = false;
	Button click;
	Button highscore;
	String clickbuttontext = "Click me";
	final Context context = this;
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);
		Button click = (Button) findViewById(R.id.startbutton);
		Button highscore = (Button) findViewById(R.id.highscorebutton);
		highscore.setOnClickListener(new View.OnClickListener() {
			public void onClick(View v) {
				Intent myIntent = new Intent(v.getContext(), highscore.class);
                startActivityForResult(myIntent, 0);           }
		});
		click.setOnClickListener(new View.OnClickListener() {
			public void onClick(View v) {
				TextView counter = (TextView) findViewById(R.id.counterview);
				counter.setText(" "+ ++count + " ");
				if(startet == false)
				{
				new CountDownTimer(10000, 1000) {
					Button click = (Button) findViewById(R.id.startbutton);
					TextView timer = (TextView) findViewById(R.id.timerview);
					public void onTick(long millisUntilFinished) {
						timer.setText("" + millisUntilFinished / 1000 + "");
					}
					public void onFinish() {
						click.setEnabled(false);
						timer.setText("Done!");
						final Dialog dialog = new Dialog(context);
						dialog.setContentView(R.layout.dialog);
						dialog.setTitle("Du vant eller tapte!");
						TextView resultat = (TextView) dialog.findViewById(R.id.counterview);
						resultat.setText("You clicked " + count + " times!");
						Button cancel = (Button) dialog.findViewById(R.id.cancelbutton);
						cancel.setOnClickListener(new View.OnClickListener() {
							public void onClick(View v) {
								dialog.dismiss();
								reset();
							}
						});
						Button save = (Button) dialog.findViewById(R.id.savebutton);
						save.setOnClickListener(new View.OnClickListener() {
							public void onClick(View v) {
								EditText name = (EditText) dialog.findViewById(R.id.nametext);
								String personname = name.getText().toString();
								connectdb();
								Cursor allrows  = scoreDB.rawQuery("SELECT * FROM highscore WHERE score > " +  count, null);
								if(allrows.getCount() < 10)
								{
									scoreDB.execSQL("INSERT INTO highscore (ID, NAME, SCORE) VALUES(null, '" + personname + "', " + count + ")");
								}
								scoreDB.close();
								dialog.dismiss();
								reset();
								Intent myIntent = new Intent(v.getContext(), highscore.class);
				                startActivityForResult(myIntent, 0);
							}
						});
						dialog.show();
					}
				}.start();
				startet = true;
				}
			}
		});
		
		Button titleback = (Button) findViewById(R.id.titlebackbutton);
		titleback.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				Intent intent = new Intent();
                setResult(RESULT_OK, intent);
				finish();	
			}
		});
	}
	public void reset()
	{
		Button click = (Button) findViewById(R.id.startbutton);
		click.setEnabled(true);
		TextView counter = (TextView) findViewById(R.id.counterview);
		TextView timer = (TextView) findViewById(R.id.timerview);
		counter.setText("0");
		timer.setText("10");
		count = 0;
		startet = false;
	}
    protected void connectdb() {
        	 scoreDB = openOrCreateDatabase("highscore.db", SQLiteDatabase.CREATE_IF_NECESSARY | SQLiteDatabase.OPEN_READWRITE, null);
        	 scoreDB.execSQL("CREATE TABLE IF  NOT EXISTS highscore (ID INTEGER PRIMARY KEY, NAME TEXT, SCORE INTEGER);");
    }
}